<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/27
 * Time: 10:07
 */
namespace app\factory\controller;
use think\Controller;
use think\Db;
class Base extends Controller
{
    public function _initialize()
    {
        if(!session('?f_uid')||!session('?f_username')){
            $this->redirect(url('factory/login/index'));
        }
    }
    
    /**
     * 极光推送--推送消息类型
     * @param $type 1:推送个人  2:推送全体
     * @param $alias 别名 user_id OR token
     * @param $message 推送消息内容
     * @param $code
     * @return array
     */
    public function Jpush1($type,$alias,$message,$code)
    {
//        $alias = (string)$alias;
        import('jpush.autoload', EXTEND_PATH);
        //初始化JPushClient
        $client = new \JPush\Client(config('jpush.appKey'),config('jpush.masterSecret'));
        //生成推送Payload构建器
        $push = $client->push();
        //推送平台 'all'  OR  ['ios','android']  OR  'ios','android'
        $push->setPlatform('all');
        //1:推送个人  2:推送全体
        if($type==1){
            $push->addAlias($alias);//按别名推送
        }else{
            $push->addAllAudience();//广播消息推送
        }
        $push->message($message, [
//            'title' => 'Hello',
            'content_type' => 'text',
            'extras' => [
                'key' => $code
            ]
        ]);
        return $push->send();
    }
}